/*
 * 编辑角色交易
 * 参数   交易码：trancode=1023,角色名：name，备注:memo
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TRole;
import org.ccams.util.Utils;
import org.json.JSONArray;

/**
 * 编辑角色交易
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 */
public class Tran1023 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("Role");
		JSONArray Roles = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newRoles = new JSONArray();
		int k = 0;
		for(int i=0;i<Roles.length();i++){
			org.json.JSONObject role = Roles.optJSONObject(i);
			if(role.has("id")){
				TRole t = new TRole();
				t.setValid(0);
				t.setId(role.getLong("id"));
				t.setName(role.getString("name"));
				t.setMemo(role.getString("memo"));
				//获得当期时间
				t.setAddDate(Utils.getCurDate());
				k = getCcamsService().getTroleDao().updateTRole(t);	//更新			
			}else{
				newRoles.put(role);
			}			
		}
		for(int i=0;i<newRoles.length();i++){
			org.json.JSONObject newRole = Roles.optJSONObject(i);
			TRole t = new TRole();
			t.setValid(0);
			t.setName(newRole.getString("name"));
			t.setMemo(newRole.getString("memo"));
			//获得当期时间
			t.setAddDate(Utils.getCurDate());
			k = getCcamsService().getTroleDao().insertTRole(t);
		}
		if (k == 1) {
			success = true;
			message = "角色保存成功";
		} else {
			success = false;
			message = "角色保存失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
